<?php


namespace app\controller\api;


use app\BaseController;

use app\common\RedisKey;
use app\facade\RedisLocationFacade;
use app\model\article\ArticleModel;

use app\model\article\NoticeModel;
use app\model\config\BannerModel;
use app\model\config\ConfigModel;
use app\model\school\SchoolModel;
use app\service\config\ConfigService;

use app\service\utils\QqMapService;
use app\service\utils\RedisService;
use think\facade\Cache;
use think\facade\Db;

class IndexController extends BaseController
{
    /**
     * 首页
     * @return \think\response\Json
     */
    public function index()
    {
        try {
            $list = BannerModel::order('sort', 'desc')->where('poisition', 1)->field('img,content,type')->select();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 附近驾校
     * @return \think\response\Json
     */
    public function getNearbySchoolList()
    {
        try {
            $page = $this->request->param('page', 1, 'int');
            $limit = $this->request->param('limit', 10, 'int');
            $lng = $this->request->param('lng', '113', 'trim');
            $lat = $this->request->param('lat', '34', 'trim');

            $locationArr = RedisLocationFacade::getMembersByDistance(RedisKey::ALL_SCHOOL_LIST, $lng, $lat, 5000);
            if (!$locationArr) {
                return $this->success();
            }
            $locationArr = RedisLocationFacade::formatLocation($locationArr);
            $schoolIdArr = $locationArr['key'];
            $distanceArr = $locationArr['value'];
            $schoolIdStr = implode(',', $schoolIdArr);
            $list = SchoolModel::field('id,img,intro,name,is_recommend,recommend_reason')
                ->whereIn('id', $schoolIdArr)
                ->orderRaw("field(id,$schoolIdStr)")
                ->paginate(['list_row' => $limit, 'page' => $page])
                ->toArray();
            foreach ($list['data'] as $key => &$value) {
                $value['distance'] = number_format($distanceArr[$value['id']], 1);
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 活动列表
     * @return \think\response\Json
     */
    public function getActiveList()
    {
        try {
            $page = $this->request->param('page', 1, 'int');
            $limit = $this->request->param('limit', 10, 'int');
            $list = ArticleModel::field('id,title,img,num,create_time')
                ->where('type', 2)
                ->order('sort', 'desc')
                ->paginate(['list_row' => $limit, 'page' => $page])
                ->toArray();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 公告详情
     * @return \think\response\Json
     */
    public function articleInfo()
    {
        try {
            $id = $this->request->param('id');
            $info = ArticleModel::find($id);
            return $this->success($info);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取富文本内容
     * @return \think\response\Json
     */
    public function getContent()
    {
        try {
            $key = $this->request->param('key', '', 'trim');
            if (is_array($key)) {
                $info = NoticeModel::whereIn('title', $key)->field('content')->select();
            } else {
                $info = NoticeModel::where('title', $key)->field('content')->find();
            }
            return $this->success($info);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 获取地址信息
     * @return \think\response\Json
     */
    public function getLocationInfo()
    {
        try {
            $lng = $this->request->param('lng', 0, 'trim');
            $lat = $this->request->param('lat', 0, 'trim');

            $key = sprintf(RedisKey::LOCATION, $lng, $lat);
            $info = Cache::get($key);
            if (!$info) {
                $qqMapService = new QqMapService();
                $info = $qqMapService->getAddressInfo($lng, $lat);
                Cache::set($key, json_encode($info), 86400);
            } else {
                $info = json_decode($info);
            }
            return $this->success($info);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    /**
     * 小程序ID
     * @return \think\response\Json
     */
    public function getAppId()
    {
        try {
            $data = config('payment.miniprogram_app_id');
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function getServiceTel()
    {
        try {
            $data = ConfigService::getConfigValue('service_tel');
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function getHint()
    {
        try {
            $data = ConfigService::getConfigValue('hint');
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function getVerifyCarIntroImg()
    {
        try {
            $data = ConfigService::getConfigValue('verify_car_intro_img');
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}